var GameController = require("GameController");

cc.Class({
    extends: cc.Component,

    properties: {
        GM: null,
        Player: null,
        Bacteria: null,
        Option: null,
        Grid: null,
        MapX:0,
        MapY:0,
        type:0,
        AdjCells:{
            default:[],
            type:cc.node
        },
        GaussCell:cc.node
    },

    // LIFE-CYCLE CALLBACKS:

    ChangeColor:function () {
        this.node.color = this.Option.Color[this.type];
        if(this.Bacteria != null){
            if(this.GaussCell.active){
                if(this.Bacteria.Color != this.type){
                    this.GaussCell.active = false;
                    this.node.color = this.Option.Color[this.type];
                }
                else {
                    this.node.color = this.Option.Color[this.type + this.Option.Color.length / 2];
                }
            }
            else{
                if(this.Bacteria.Color == this.type){
                    this.GaussCell.active = true;
                    this.node.color = this.Option.Color[this.type + this.Option.Color.length / 2];
                }
                else{
                    this.node.color = this.Option.Color[this.type];
                }
            }
        }
    },
    // 消除
    Clear:function () {        
        if(this.MapX == this.Option.MapGridSize && this.MapY == this.Option.MapGridSize){
            this.scheduleOnce(this.ResetMap, 2);
        }
        else
        this.node.active = false;
    },
    //地图重置
    ResetMap:function () {
        this.GM.Initiate.InitiateCells(this.GM.Initiate);
    },
    // 绑定周围的细胞   
    BindAdjCell:function () {           
        this.AdjCells[0] = this.Grid.MapGrid[this.MapX][this.MapY + 1];
        this.AdjCells[1] = this.Grid.MapGrid[this.MapX + 1][this.MapY + 1] || null;
        this.AdjCells[2] = this.Grid.MapGrid[this.MapX + 1][this.MapY] || null;
        this.AdjCells[3] = this.Grid.MapGrid[this.MapX][this.MapY - 1] || null;
        this.AdjCells[4] = this.Grid.MapGrid[this.MapX - 1][this.MapY - 1] || null;
        this.AdjCells[5] = this.Grid.MapGrid[this.MapX - 1][this.MapY] || null;
    },

    ChangeCollisionSize:function () {
        //改变碰撞体半径
    },

    onLoad () {
        this.GaussCell =  this.node.getChildByName("GaussCell");
        this.GaussCell.active = false;
    },

    start () {

    },

    update (dt) {
        if(this.Bacteria != null){
            if(this.GaussCell.active){
                if(this.Bacteria.Color != this.type){
                    this.GaussCell.active = false;
                    this.node.color = this.Option.Color[this.type];
                }
            }
            else{
                if(this.Bacteria.Color == this.type){
                    this.GaussCell.active = true;
                    this.node.color = this.Option.Color[this.type + this.Option.Color.length / 2];
                }
            }
        }
    },
});
